package com.example.minefieldkeepercloud.activity.machinerymaintain;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;


import android.support.v7.app.AppCompatActivity;


import android.view.View;
import android.view.ViewGroup;

import android.widget.LinearLayout;

import android.widget.TextView;


import com.example.minefieldkeepercloud.R;


import com.example.minefieldkeepercloud.activity.machinerymaintain.fragment.ChartFragment;

import com.example.minefieldkeepercloud.activity.machinerymaintain.fragment.ListFragment;


import com.example.minefieldkeepercloud.view.lazyviewpager.LazyFragmentPagerAdapter;
import com.example.minefieldkeepercloud.view.lazyviewpager.LazyViewPager;


import com.example.minefieldkeepercloud.view.pop.TypeSelectPopupWindow;


import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 矿机维护
 */
public class MachineMaintain extends AppCompatActivity {
    @BindView(R.id.viewpager)
    LazyViewPager viewpager;
    @BindView(R.id.tv_date)
    TextView tv_date;
    @BindView(R.id.ll_date)
    LinearLayout ll_date;

    private CustomLazyFragmentPagerAdapter customLazyFragmentPagerAdapter;
    private TypeSelectPopupWindow typeSelectPopupWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.machinemaintain);
        ButterKnife.bind(this);
        initview();

    }

    /**
     * 初始化
     */
    private void initview() {
        customLazyFragmentPagerAdapter = new CustomLazyFragmentPagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(customLazyFragmentPagerAdapter);
        typeSelectPopupWindow = new TypeSelectPopupWindow(1,this, new TypeSelectPopupWindow.OnSelectLayoutListener() {
            @Override
            public void selectTypeLayout(int i) {

                if (i == 0) {
                    tv_date.setText("统计图");
                    viewpager.setCurrentItem(0, false);

                } else if (i == 1) {
                    tv_date.setText("统计表");
                    viewpager.setCurrentItem(1, false);

                }

            }
        });

        tv_date.setText("统计图");

    }


    @OnClick({R.id.title_left, R.id.ll_date})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_left:
                finish();
                break;
            case R.id.ll_date:
                if (typeSelectPopupWindow != null)
                    typeSelectPopupWindow.showAsDropDown(ll_date, 0, 0);
                break;

        }
    }


    private static class CustomLazyFragmentPagerAdapter extends LazyFragmentPagerAdapter {

        private CustomLazyFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(ViewGroup container, int position) {
            if (position == 1) {
                return new ListFragment();
            } else if (position == 0) {
                return new ChartFragment();
            }
            return null;
        }

        @Override
        public int getCount() {
            return 2;
        }

    }
}
